#include "Page.h"
#include "User/OLED/OLED.h"
#include "PageConfig.h"

static uint8_t last_select;
static uint8_t last_time_select;
static uint8_t show_select_line;
static uint32_t last_page_state_flag;
static uint32_t page_state_flag = 0;

void Page_FlagShift(uint32_t flag)  {CHANGE_BIT(page_state_flag, flag);}
void Page_FlagSet(uint32_t flag)    {SET_BIT(page_state_flag, flag);}
void Page_FlagReset(uint32_t flag)  {CLEAR_BIT(page_state_flag, flag);}
bool Page_FlagGet(uint32_t flag)    {return IS_BIT_1(page_state_flag, flag);}

static void _show_16x16(uint8_t row, uint8_t column, uint8_t str[][16], uint8_t num)
{
    uint8_t i,j;
    for(i=0;i<num;i++)
    {
        OLED_SetCursor((row - 1)*2, (column - 1 + i)*16); // 设置光标位置在上半部分
        for (j = 0; j < 16; j++)
        {
            OLED_WriteData(str[i*2][j]); // 显示上半部分内容
        }
        OLED_SetCursor((row - 1)*2+1, (column - 1 + i)*16); // 设置光标位置在上半部分
        for (j = 0; j < 16; j++)
        {
            OLED_WriteData(str[i*2+1][j]); // 显示下半部分内容
        }
    }
}

void Page_indexInit()
{
    _show_16x16(2, 1, index_page, 8);
    OLED_ShowChar(3, 7, ':');
    OLED_ShowChar(3, 10, ':');
}

void Page_indexTime(uint8_t nowhour, uint8_t nowminute, uint8_t nowsecond)
{
    OLED_ShowNum(3, 5, nowhour, 2);
    OLED_ShowNum(3, 8, nowminute, 2);
    OLED_ShowNum(3, 11, nowsecond, 2);
    if(Page_FlagGet(PageChildLock_Flag))
    {
        _show_16x16(4, 4, lock, 2);
    }
    else
    {
        OLED_ShowString(4, 7, "    ");
    }
}

void Page_selectInit()
{
    OLED_Clear();
    last_select = -1;
}

void Page_showSelect(uint8_t select)
{
    select = select % 5;

    if(select == last_select && page_state_flag == last_page_state_flag)
        return;

    if(select == 0)
    {
        show_select_line = 0;
    }
    else if(select - show_select_line >= 4)
    {
        show_select_line += 1;
    }

    if(show_select_line == 0)
    {
        _show_16x16(1, 1, auto_feed, 4);
        OLED_ShowString(1, 9, "        ");
        if(Page_FlagGet(PageAutoFeed_Flag))
        {
            _show_16x16(1, 7, check, 1); // 打勾设置
        }
    }
    if(show_select_line <= 1)
    {
        _show_16x16(1 - show_select_line+1, 1, set_feed_time, 6);
        OLED_ShowString(1 - show_select_line+1, 13, "    ");
    }
    if(show_select_line <= 2)
    {
        _show_16x16(2 - show_select_line+1, 1, sound_turn, 4);
        OLED_ShowString(2 - show_select_line+1, 9, "        ");
        if(Page_FlagGet(PageSound_Flag))
        {
            _show_16x16(2 - show_select_line+1, 7, check, 1); // 打勾设置
        }
    }
    if(show_select_line <= 3)
    {
        _show_16x16(3 - show_select_line+1, 1, set_feed_size, 6);
        OLED_ShowString(3 - show_select_line+1, 13, "    ");
    }
    if(show_select_line <= 4 && show_select_line >= 1)
    {
        _show_16x16(4 - show_select_line+1, 1, child_lock, 2);
        OLED_ShowString(4 - show_select_line+1, 5, "            ");
        if(Page_FlagGet(PageChildLock_Flag))
        {
            _show_16x16(4 - show_select_line+1, 7, check, 1); // 打勾设置
        }
    }

    _show_16x16(select - show_select_line + 1, 8, arror, 1);

    last_select = select;
    last_page_state_flag = page_state_flag;
}

void Page_feedTimeInit()
{
    OLED_Clear();
    last_time_select = -1;
}

static bool _same(uint8_t feedTime[][3], uint8_t length)
{
    uint8_t i;
    for(i=0;i<length;i++)
    {

    }
}

void Page_feedTimeFresh()
{
    last_time_select = -1;
}

void Page_showSetFeedTime(uint8_t time_select, uint8_t feedTime[][3])
{
    if(time_select == last_time_select && page_state_flag == last_page_state_flag)
        return;

    /* 第一行 */
    _show_16x16(1, 1, feed_time_page, 4);
    OLED_ShowString(1, 9, "        ");
    if(Page_FlagGet(PageFeedTimeHour_Flag))
    {
        _show_16x16(1, 7, hour, 1);
    }
    else if(Page_FlagGet(PageFeedTimeMinute_Flag))
    {
        _show_16x16(1, 7, minute, 1);
    }
    else if(Page_FlagGet(PageFeedTimeSecond_Flag))
    {
        _show_16x16(1, 7, second, 1);
    }

    /* 第二行 */
    OLED_ShowNum(2, 1, feedTime[0][0], 2);
    OLED_ShowChar(2, 3, ':');
    OLED_ShowNum(2, 4, feedTime[0][1], 2);
    OLED_ShowChar(2, 6, ':');
    OLED_ShowNum(2, 7, feedTime[0][2], 2);
    OLED_ShowString(2, 9, "        ");
    if(Page_FlagGet(PageFeedTime0On_Flag))
    {
        _show_16x16(2, 7, check, 1);
    }

    /* 第三行 */
    OLED_ShowNum(3, 1, feedTime[1][0], 2);
    OLED_ShowChar(3, 3, ':');
    OLED_ShowNum(3, 4, feedTime[1][1], 2);
    OLED_ShowChar(3, 6, ':');
    OLED_ShowNum(3, 7, feedTime[1][2], 2);
    OLED_ShowString(3, 9, "        ");
    if(Page_FlagGet(PageFeedTime1On_Flag))
    {
        _show_16x16(3, 7, check, 1);
    }

    /* 第四行 */
    OLED_ShowNum(4, 1, feedTime[2][0], 2);
    OLED_ShowChar(4, 3, ':');
    OLED_ShowNum(4, 4, feedTime[2][1], 2);
    OLED_ShowChar(4, 6, ':');
    OLED_ShowNum(4, 7, feedTime[2][2], 2);
    OLED_ShowString(4, 9, "        ");
    if(Page_FlagGet(PageFeedTime2On_Flag))
    {
        _show_16x16(4, 7, check, 1);
    }

    _show_16x16(time_select + 1, 8, arror, 1);

    last_time_select = time_select;
    last_page_state_flag = page_state_flag;
}

void Page_feedSizeInit()
{
    OLED_Clear();
    last_time_select = -1;
}

void Page_showSetFeedSize(uint8_t feed_select, uint8_t feedSize[2])
{
    if(feed_select == last_time_select && page_state_flag == last_page_state_flag)
        return;

    /* 第一行 */
    _show_16x16(1, 1, set_feed_size, 6);
    OLED_ShowString(1, 13, "    ");

    /* 第二行 */
    _show_16x16(2, 1, set_feed_size+4, 4);
    OLED_ShowString(2, 9, "  ");
    OLED_ShowNum(2, 11, feedSize[0], 1);
    OLED_ShowString(2, 12, "s    ");

    /* 第三行 */
    _show_16x16(3, 1, set_water_height, 4);
    OLED_ShowString(3, 9, "  ");
    OLED_ShowNum(3, 11, feedSize[1], 1);
    OLED_ShowString(3, 12, "l    ");

    _show_16x16(feed_select + 1, 8, arror, 1);

    last_time_select = feed_select;
    last_page_state_flag = page_state_flag;
}

void Page_feedSizeFresh()
{
    last_time_select = -1;
}

// uint8_t const *const Page_getFeedTime(uint8_t feed_time_x)
// {
//     switch (feed_time_x)
//     {
//     case 0:
//         return FeedTime0;
//     case 1:
//         return FeedTime1;
//     case 2:
//         return FeedTime2;
//     default:
//         return FeedTime0;
//     }
// }
